package lib.drumpicker;


import jp.jig.test.alarm.ViewUtil;
import android.content.Context;
import android.util.AttributeSet;

public class TimeDrumPicker extends DrumPicker {
	private final static String TAG = TimeDrumPicker.class.getSimpleName();



	private static final String[] HOUR_LIST = new String[] {
		"00",
		"01",
		"02",
		"03",
		"04",
		"05",
		"06",
		"07",
		"08",
		"09",
		"10",
		"11",
		"12",
		"13",
		"14",
		"15",
		"16",
		"17",
		"18",
		"19",
		"20",
		"21",
		"22",
		"23",
	};

	private static String[] MIN_LIST;
	private String mSelectedHour;
	private String mSelectedMin;

	public TimeDrumPicker(Context context) {
		this(context, null);
	}

	public TimeDrumPicker(Context context, AttributeSet attr) {
		super(context, attr);
		init();

	}

	public void init() {

		MIN_LIST = new String[60];
		for (int i = 0; i < 60; i++) {
			StringBuffer sb = new StringBuffer();
			sb.append(i / 10).append(i % 10);
			MIN_LIST[i] = sb.toString();
		}

		// onsizeChangedでやったら？
		addRow(HOUR_LIST, 140);
		addRow(MIN_LIST, 140);
		setPosition(0, 0);
		setPosition(1, 0);
		setOnPostionChangedListener(new OnPositionChangedListener() {
			@Override
			public void onPositionChanged(int itemPos, int pos) {
				if (itemPos == 0) {
					mSelectedHour = HOUR_LIST[pos];
				} else if (itemPos == 1) {
					mSelectedMin = MIN_LIST[pos];
				}
			}
		});
	}

	/**
	 * 時間を返します
	 * 時間*100 + 分
	 * 
	 * @return
	 */
	public int getTime() {
		return Integer.parseInt(mSelectedHour + mSelectedMin);
	}

	public void setTime(int hour, int min) {
		// mSelectedHour = HOUR_LIST[hour];
		// mSelectedMin = MIN_LIST[min];
		setPosition(0, hour);
		setPosition(1, min);
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int height = ViewUtil.getDip2Pixel(getContext(), DrumPicker.LINE_HEIGHT * 3);
		int h = MeasureSpec.makeMeasureSpec(MeasureSpec.getMode(heightMeasureSpec), height);
		super.onMeasure(widthMeasureSpec, h);
	}
}
